package com.ziven.common.widget.value;

import android.view.View;

import androidx.annotation.DimenRes;
import androidx.annotation.NonNull;

import com.ziven.common.widget.WidgetUtils;
import com.ziven.easygo.util.EasyUtils;
import com.ziven.easygo.util.ResourceUtils;
import com.ziven.easygo.util.ViewHelper;

/**
 * @author Ziven
 */
public final class TitleMargin extends MixValue {

    private static final int SIZE = 4;
    private static final int NONE = -101;

    public static TitleMargin topDimenId(@DimenRes int topDimenId) {
        return size(
                DimenValue.topDimenId(topDimenId)
        );
    }

    public static TitleMargin bottomDimenId(@DimenRes int bottomDimenId) {
        return size(
                DimenValue.bottomDimenId(bottomDimenId)
        );
    }

    public static TitleMargin topBottomDimenId(@DimenRes int marginDimenId) {
        int dimen = ResourceUtils.getDimen(marginDimenId);
        return topBottom(dimen, dimen);
    }

    public static TitleMargin topBottomDimenId(@DimenRes int topDimenId,
                                               @DimenRes int bottomDimenId) {
        return size(
                DimenValue.topDimenId(topDimenId),
                DimenValue.bottomDimenId(bottomDimenId)
        );
    }

    public static TitleMargin startDimenId(@DimenRes int startDimenId) {
        return size(
                DimenValue.startDimenId(startDimenId)
        );
    }

    public static TitleMargin endDimenId(@DimenRes int endDimenId) {
        return size(
                DimenValue.endDimenId(endDimenId)
        );
    }

    public static TitleMargin startEndDimenId(@DimenRes int marginDimenId) {
        int dimen = ResourceUtils.getDimen(marginDimenId);
        return startEnd(dimen, dimen);
    }

    public static TitleMargin startEndDimenId(@DimenRes int startDimenId,
                                              @DimenRes int endDimenId) {
        return size(
                DimenValue.startDimenId(startDimenId),
                DimenValue.endDimenId(endDimenId)
        );
    }

    public static TitleMargin dimenId(@DimenRes int dimenId) {
        int dimen = ResourceUtils.getDimen(dimenId);
        return size(dimen, dimen, dimen, dimen);
    }

    public static TitleMargin dimenId(@DimenRes int topDimenId,
                                      @DimenRes int bottomDimenId,
                                      @DimenRes int startDimenId,
                                      @DimenRes int endDimenId) {
        return size(
                DimenValue.topDimenId(topDimenId),
                DimenValue.bottomDimenId(bottomDimenId),
                DimenValue.startDimenId(startDimenId),
                DimenValue.endDimenId(endDimenId)
        );
    }

    public static TitleMargin top(@NonNull String top) {
        return size(
                DimenValue.topSize(top)
        );
    }

    public static TitleMargin bottom(@NonNull String bottom) {
        return size(
                DimenValue.bottomSize(bottom)
        );
    }

    public static TitleMargin topBottom(@NonNull String margin) {
        int dimen = WidgetUtils.dimenInt(margin);
        return topBottom(dimen, dimen);
    }

    public static TitleMargin topBottom(@NonNull String top,
                                        @NonNull String bottom) {
        return size(
                DimenValue.topSize(top),
                DimenValue.bottomSize(bottom)
        );
    }

    public static TitleMargin start(@NonNull String start) {
        return size(
                DimenValue.startSize(start)
        );
    }

    public static TitleMargin end(@NonNull String end) {
        return size(
                DimenValue.endSize(end)
        );
    }

    public static TitleMargin startEnd(@NonNull String margin) {
        int dimen = WidgetUtils.dimenInt(margin);
        return startEnd(dimen, dimen);
    }

    public static TitleMargin startEnd(@NonNull String start,
                                       @NonNull String end) {
        return size(
                DimenValue.startSize(start),
                DimenValue.endSize(end)
        );
    }

    public static TitleMargin size(@NonNull String margin) {
        int dimen = WidgetUtils.dimenInt(margin);
        return size(dimen, dimen, dimen, dimen);
    }

    public static TitleMargin size(@NonNull String top,
                                   @NonNull String bottom,
                                   @NonNull String start,
                                   @NonNull String end) {
        return size(
                DimenValue.topSize(top),
                DimenValue.bottomSize(bottom),
                DimenValue.startSize(start),
                DimenValue.endSize(end)
        );
    }

    public static TitleMargin top(int top) {
        return size(
                DimenValue.topSize(top)
        );
    }

    public static TitleMargin bottom(int bottom) {
        return size(
                DimenValue.bottomSize(bottom)
        );
    }

    public static TitleMargin topBottom(int margin) {
        return topBottom(margin, margin);
    }

    public static TitleMargin topBottom(int top, int bottom) {
        return size(
                DimenValue.topSize(top),
                DimenValue.bottomSize(bottom)
        );
    }

    public static TitleMargin start(int start) {
        return size(
                DimenValue.startSize(start)
        );
    }

    public static TitleMargin end(int end) {
        return size(
                DimenValue.endSize(end)
        );
    }

    public static TitleMargin startEnd(int margin) {
        return startEnd(margin, margin);
    }

    public static TitleMargin startEnd(int start, int end) {
        return size(
                DimenValue.startSize(start),
                DimenValue.endSize(end)
        );
    }

    public static TitleMargin size(int margin) {
        return size(margin, margin, margin, margin);
    }

    public static TitleMargin size(int top, int bottom, int start, int end) {
        return size(
                DimenValue.topSize(top),
                DimenValue.bottomSize(bottom),
                DimenValue.startSize(start),
                DimenValue.endSize(end)
        );
    }

    public static TitleMargin size(@NonNull DimenValue... values) {
        return new TitleMargin(values);
    }

    private TitleMargin(DimenValue... values) {
        super(values);
    }

    @Override
    protected void takeEffect(@NonNull ViewHelper<? extends View> helper, @NonNull Value[] values) {
        helper.getViewNulls().doNotNull(view -> setTitleMargin(helper, values));
    }

    private void setTitleMargin(@NonNull ViewHelper<? extends View> helper,
                                @NonNull Value[] values) {
        if(setTitleMargin4(helper, values)) {
            return;
        }
        EasyUtils.forEach(values,
                value -> value.myself(DimenValue.class,
                        dimen -> setTitleMargin(helper, dimen)));
    }

    private boolean setTitleMargin4(@NonNull ViewHelper<? extends View> helper,
                                    @NonNull Value[] values) {
        if(values.length != SIZE) {
            return false;
        }
        int[] intValues = {NONE, NONE, NONE, NONE};
        EasyUtils.forEach(values,
                value -> value.myself(DimenValue.class,
                        dimenValue -> {
                            int index = dimenValue.direction();
                            if(index >= DimenValue.START && index <= DimenValue.BOTTOM) {
                                dimenValue.value(Integer.class,
                                        intValue -> intValues[index] = intValue);
                            }
                }));
        for (int value : intValues) {
            if(value == NONE) {
                return false;
            }
        }
        helper.setTitleMargin(
                intValues[DimenValue.START],
                intValues[DimenValue.TOP],
                intValues[DimenValue.END],
                intValues[DimenValue.BOTTOM]
        );
        return true;
    }

    private void setTitleMargin(@NonNull ViewHelper<? extends View> helper,
                                @NonNull DimenValue dimenValue) {
        switch (dimenValue.direction()) {
            case DimenValue.TOP:
                helper.setTitleMarginTop(dimenValue.value());
                break;
            case DimenValue.BOTTOM:
                helper.setTitleMarginBottom(dimenValue.value());
                break;
            case DimenValue.START:
                helper.setTitleMarginStart(dimenValue.value());
                break;
            case DimenValue.END:
                helper.setTitleMarginEnd(dimenValue.value());
                break;
            default:
                break;
        }
    }
}
